package com.yoomiito.app.ui.anyview.sureorder

import android.content.Context
import android.text.TextUtils
import android.view.View
import cn.droidlover.xdroidmvp.cache.SharedPref
import com.alibaba.fastjson.JSON
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.model.bean.AddressInfo
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.order.CreateOrderData
import com.yoomiito.app.model.order.ShopCarGoods
import com.yoomiito.app.ui.anyview.order.ShowAddressView
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.utils.inputfilter.TextInputFilter
import kotlinx.android.synthetic.main.item_order_goods.view.*
import kotlinx.android.synthetic.main.item_order_num.view.*
import kotlinx.android.synthetic.main.item_remark.view.*
import kotlinx.android.synthetic.main.view_sure_order_info.view.*

class YouPinSureOrderView(context: Context, mGoodsDetail: GoodsDetail) : BaseSureOrderView(context, mGoodsDetail) {

    private lateinit var mShowAddressView: ShowAddressView

    override fun getViewId(): Int = R.layout.view_sure_order_info

    override fun initView() {
        mShowAddressView = ShowAddressView(context)
        addressParentView.addView(mShowAddressView)

        val ad = SharedPref.getInstance(context).getString(Constant.Address, "")
        if (!TextUtils.isEmpty(ad)) {
            mAddressInfo = JSON.parseObject(ad, AddressInfo::class.java)
            mShowAddressView.setAddressInfo(mAddressInfo)
            mShowAddressView.showAddress()
        }

        goods_size_control_view.visibility = View.GONE
        ImageLoader.getInstance().into(mContext, mGoodsDetail.pict_url, item_order_goods_iv)
        item_order_goods_title.text = mGoodsDetail.title
        item_order_goods_price.text = "¥" + mGoodsDetail.salePrice
        act_buy_goods_notes.filters = arrayOf(TextInputFilter(true).setLength(20))
        mGoodsTotalPrice = AllUtils.priceMul_2(mGoodsDetail.salePrice, 1)
        val s = "合计: ¥$mGoodsTotalPrice"
        val p = s.indexOf("¥")
        spanText = SpannableUtils.getSizeAndPriceText(s, R.color.color_ff5100, p, s.length, p, p + 1)
        mOnShowInfoListener(spanText!!, 1, 0)
    }

    override fun updateAddress(addressInfo: AddressInfo) {
        mAddressInfo = addressInfo
        mShowAddressView.setAddressInfo(mAddressInfo)
        mShowAddressView.showAddress()
    }

    override fun orderData(): CreateOrderData = CreateOrderData(
            mAddressInfo?.userName,
            mAddressInfo?.mobile,
            mAddressInfo?.address,
            mAddressInfo?.provinceName + mAddressInfo?.cityName + mAddressInfo?.countyName,
            mAddressInfo?.provinceCode,
            mAddressInfo?.cityCode,
            mAddressInfo?.countyCode,
            listOf(ShopCarGoods(mGoodsDetail.num_iid, 1, act_buy_goods_notes.text.toString()))
    )
}